import baseApiUrl from "@/utils/baseConf.js"

// 声明一个对象用于存储请求个数
//var needLoadingRequestCount = 0;

/* // 显示加载动画
function showFullScreenLoading() {
	if (needLoadingRequestCount === 0) {
		startLoading();
	}
	needLoadingRequestCount++;
};

//隐藏加载动画
function tryHideFullScreenLoading() {
	if (needLoadingRequestCount <= 0) return;
	needLoadingRequestCount--;
	if (needLoadingRequestCount === 0) {
		endLoading();
	}
}; */
//判断是安卓还是ios
let devlType = '';
switch (uni.getSystemInfoSync().platform) {
	case 'android':
		devlType = 'android'
		break;
	case 'ios':
		devlType = 'ios'
		break;
	default:
		devlType = 'ios'
		break;
}

async function fun(url, method, data, isloading = true, header = {}) {
	url = url || '';
	data = data || {};
	method = method || 'GET';
	let token = uni.getStorageSync('token') || '';
	//验签----------------------

	// header.platform = devlType
	let sign = '123456'
	//验签end----------------------
	let getVersion = wx.getAccountInfoSync().miniProgram.version; //线上小程序版本号
	let defaultHeader = {
		"Content-Type": "application/json",
		sign: sign,
		token: token,
		platform: devlType,
		version: getVersion || ''
	};
	header = Object.assign({}, defaultHeader, header);
	return new Promise(function(resolve, reject) {
		//显示加载动画
		// if (loading) utils.showLoading()
		//isloading && showFullScreenLoading();
		uni.request({
			url: baseApiUrl + url,
			header: header,
			data: data,
			timeout: 10000,
			method: method,
			dataType: 'json',
			success: (res) => {
				console.log(res)
				if (!data.msg) { //动态详情返回上一页时不显示提示信息
					if (res.data?.message || res.data?.data?.tips) {
						uni.showToast({
							title: res.data.message || res.data?.data?.tips,
							icon: "none",
							mask: true
						});
					}
				}

				if (res.statusCode === 200) {
					if (res.data?.code == 200000) {
						resolve(res.data?.data)
					} else {
						if (!res.data) {
							uni.showToast({
								title: '未知错误',
								icon: "none"
							});
						}
						reject(res.data?.message || '未知错误')
					}
				} else {
					if (["401000", "401002", "401003", "401004", "401005"].includes(res.data
							?.code)) {
						uni.removeStorageSync('token');
						//setTimeout(() => {
						var pages = getCurrentPages(); //获取加载的页面
						var url = pages[0].route; //获取当前页面url
						console.log(url, url.includes('pages/login/index'))
						//Vue.prototype.$uni.Lastrouter = url;
						if (url.includes('pages/login/index')) {
							return; //全局判断,如果已经在login页,就没必要在返回登录页了
						}
						uni.reLaunch({
							url: '/pages/login/index'
						})
						//}, 1000)
					}
					reject(res.data) // 401未授权。。。
				}
				//tryHideFullScreenLoading();
			},
			fail: (err) => {
				console.log('https-err:', err);
				//tryHideFullScreenLoading();
				if (err.errMsg.indexOf("-105") !== -1) {
					uni.showToast({
						title: '网络异常',
						icon: 'none'
					})
					reject('105');
				} else {
					uni.showToast({
						title: err.errMsg,
						icon: 'none'
					})
					reject(err)
				}
			},
			complete: (com) => {
				//结束加载动画
				//tryHideFullScreenLoading();
			}
		})
	});
}

// 腾讯云桶 - 参数配置
let baseConfig = {};

baseConfig = {}

export default {
	"get": function(url, data, isloading, header) {
		return fun(url, "GET", data, isloading, header);
	},
	"post": function(url, data, isloading, header) {
		return fun(url, "POST", data, isloading, header);
	},
	"delete": function(url, data, isloading, header) {
		return fun(url, "DELETE", data, isloading, header);
	},
	"put": function(url, data, isloading, header) {
		return fun(url, "PUT", data, isloading, header);
	}
};
